package org.zoomdev.zoom.web.test;

import org.zoomdev.zoom.common.utils.CollectionUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;



public class HttpServletRequestImpl {

    private final Map<String, Object> data = new HashMap<>();
    private final Map<String, List<String>> headers = new HashMap<>();
    private final Map<String, String> parameters = new HashMap<>();
    // 暂时不管session
    private final HttpSession session = new MockHttpSession();
    private String servletPath;
    private String method;
    private String contentType;
    private byte[] content;
    private String encoding;
    private ServletInputStream inputStream;

    private String queryString;

    public HttpServletRequestImpl(String url, String method) {
        this.method = method;

        // 分割路径和查询参数
        String[] urlParts = url.split("\\?", 2);
        servletPath = urlParts[0];
        queryString = urlParts.length > 1 ? urlParts[1] : "";

        // 解析查询参数到Map
        Hashtable<String, String[]> parameters = HttpUtils.parseQueryString(queryString);
        for(Map.Entry<String,String[]> entry : parameters.entrySet()){
            this.parameters.put(entry.getKey(),entry.getValue()[0]);
        }
    }


    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> map = new HashMap<>();
        for (Map.Entry<String, String> entry : this.parameters.entrySet()) {
            map.put(entry.getKey(), new String[]{entry.getValue()});
        }
        return map;
    }

    public HttpSession getSession() {
        return session;
    }


    public ServletInputStream getInputStream() throws IOException {
        return inputStream;
    }

    public String getCharacterEncoding() {
        return encoding;
    }

    public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
        this.encoding = env;
    }

    public String getParameter(String key) {
        return parameters.get(key);
    }

    public void setParameters(Map<String, String> data) {
        if (data == null) {
            return;
        }
        this.parameters.putAll(data);
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String type) {
        headers.put("Content-Type", CollectionUtils.asList(type));
        contentType = type;
    }

    public void setHeader(String key, String value) {
        this.headers.put(key, Collections.singletonList(value));
    }

    public String getHeader(String key) {
        List<String> values = headers.get(key);
        if (values != null) {
            if (values.size() > 0) {
                return values.get(0);
            }
        }
        return null;
    }

    public String getServletPath() {
        return servletPath;
    }

    public void setServletPath(String servletPath) {
        this.servletPath = servletPath;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public void setAttribute(String var1, Object var2) {
        data.put(var1, var2);
    }

    public Object getAttribute(String var1) {
        return data.get(var1);
    }


    public void removeAttribute(String var1) {
        data.remove(var1);
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
        this.inputStream = MockUtils.inputStream(content);
    }
}